<?php
class V{
	private $fieldRules = array();
	private $result = null;
	private $op = array(
		'required', //不为空验证
		'max', //长度验证
		'min', 
		'email', //邮箱验证
		'num', //数字验证
		'reg', //正则字符串
		'compare' //对比
		);

	/**
	 * 初始化验证规则
	 * 
	 * @param $rules
	 * @return 
	 * @since v1.0
	 * @create 2012-07-12
	 */
	private function _init_rule($rules){
		// 验证规则为空
		if( !is_array($rules) || count($rules)<1 ) return false;
		// 清空旧规则
		$fieldRules = array();
		// 重新设置规则
		foreach( $rules as $field=>$rule ){
			foreach( $rule as $k=>$v ){
				// 过滤验证操作
				if( !in_array($k, $this->op) ) unset($rule[$k]);
			}
			if( is_array($rule) ) $this->fieldRules[$field] = $rule;
		}
	}

	/**
	 * 验证操作
	 * 
	 * @param array $data, 需要验证的数组
	 * @param array $rules, 验证规则, 格式：$arrRules['name'] = array(
									'required' => true,
									'max' => 10,
									'min' => 2,
									'email' => true,
									'num' => true,
									'reg' => "/^\d{13}$/",
									'compare' => 'some text',
									);	
	 						
	 * @return bool
	 * @since v1.0
	 * @create 2012-07-12
	 */
	public function valid(&$data, $rules){
		// 验证数据或者规则为空
		if( !is_array($data) || !is_array($rules) ) return false;
		// 初始化验证规则
		$this->_init_rule( $rules );
		$result = array();
		foreach( $data as $k=>$v ){
			if( isset($this->fieldRules[$k]) ){
				foreach( $this->fieldRules[$k] as $op=>$val ){
					if( $op=='required' ){
						$result[$k][$op] = $this->required($v);
					}elseif( $op=='max' && !empty($v) ){
						$result[$k][$op] = $this->len($v, $val);
					}elseif( $op=='min' && !empty($v) ){
						$result[$k][$op] = $this->len($v, $val, 'min');
					}elseif( $op=='email' && !empty($v) ){
						$result[$k][$op] = $this->email($v);
					}elseif( $op=='num' && !empty($v) ){
						$result[$k][$op] = $this->number($v);
					}elseif( $op=='reg' && !empty($v) ){
						$result[$k][$op] = $this->reg($v, $val);
					}elseif( $op=='compare' && !empty($v) ){
						$result[$k][$op] = $this->compare($v, $val);
					}
				}	
			}
		}
		$this->result = $result;
		foreach( $result as $k=>$v ){
			if( in_array(false,$result[$k]) ){
			 	return false;
			}
			return true;
		}
	}

	public function get_valid_detail(){
		return $this->result;
	}
	/**
	 * 空验证
	 * 
	 * @param mix $val 验证字符串
	 * @param bool $null 是否允许NULL值 
	 * @return bool
	 * @since v1.0
	 * @create 2012-07-12
	 */
	public function required($val, $null=false){
		if( $null ){
			return (!is_null($string) && empty($string));
		}else{
			return empty($string);
		}
	}

	/**
	 * 长度验证
	 * 
	 * @param string $string
	 * @param int $len
	 * @param string $op 'max'最大长度验证，'min'最小长度验证
	 * @return 
	 * @since v1.0
	 * @create 2012-07-12
	 */
	public function len($string, $len, $op='max'){
		if( function_exists('mb_strlen') ){
			$strlen = mb_strlen($string,'utf-8');
		}else{
			$strlen = strlen($string);
		}
		return ($op=='max') ? ($strlen<=intval($len)) : ($strlen>=intval($len));
	}
	
	/**
	 * email地址验证
	 * 
	 * @param $string
	 * @return 
	 * @since v1.0
	 * @create 2012-07-12
	 */
	public function email($string){
		$pattern = "/^\w+@\S{2,}\.[a-z]{2,}/i";
		return (bool)preg_match($pattern, $string);
	}

	/**
	 * 数字验证
	 * 
	 * @param $val
	 * @return 
	 * @since v1.0
	 * @create 2012-07-12
	 */
	public function number($val){
		return is_numeric( $val );
	}


	/**
	 * 正则
	 * 
	 * @param $var
	 * @param $pattern
	 * @return 
	 * @since v1.0
	 * @create 2012-07-12
	 */
	public function reg($var, $pattern){
		return (bool)preg_match($pattern, $var);
	}

	/**
	 * 比较两个参数
	 * 
	 * @param $val
	 * @param $val2
	 * @param $strict
	 * @return 
	 * @since v1.0
	 * @create 2012-07-12
	 */
	public function compare($val, $val2, $strict=false){
		if( $strict ){
			return $val===$val2;
		}
		return $val==$val2;
	}
}

